package com.inossem.warehousestorage_inossem.adapter.storage;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.storage.StorageManageExtra;
import com.inossem.warehousestorage_inossem.activity.storage.StorageManageMatrActivity;
import com.inossem.warehousestorage_inossem.activity.storage.StorageManageMatrMoveActivity;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMaterielIndexBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageMaterielBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.utils.StorageManageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

//import com.inossem.warehousestorage_inossem.activity.storage.StorageManageMatrMoveActivity;

/**
 * Created by Administrator on 2017/7/16 0016.
 */

public class StorageManageMatrAdapter extends BaseAdapter {

    private Context mContext;

    private LayoutInflater inflater;

    private EditText input;

    private String mFactoryCode, mFactoryName, mInventoryAddressCode, mInventoryAddressName, mCondition;

    private ImageView search;

    private List<StorageManageMatrBean.BodyBean> mMaterielList;

    private List<StorageManageMaterielIndexBean.BodyBean> mFactoryList;

    private StorageManageMatrActivity.StorageManageMaterielListener mStorageManageMaterielListener;

    private TextView factory, inventoryAddress;

    public StorageManageMatrAdapter(Context context, StorageMaterielBean bean, String condition, StorageManageMatrActivity.StorageManageMaterielListener storageManageMaterielListener) {
        mContext = context;
        mMaterielList = bean.getMaterielList();
        mFactoryList = bean.getFactoryList();
        mFactoryCode = bean.getFactoryCode();
        mFactoryName = bean.getFactoryName();
        mInventoryAddressCode = bean.getInventoryAddressCode();
        mInventoryAddressName = bean.getInventoryAddressName();
        mCondition = condition;
        mStorageManageMaterielListener = storageManageMaterielListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mMaterielList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mMaterielList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_storage_manage_list_first_item, null);
            factory = (TextView) convertView.findViewById(R.id.factory);
            inventoryAddress = (TextView) convertView.findViewById(R.id.inventoryAddress);
            PercentRelativeLayout factoryHide = (PercentRelativeLayout) convertView.findViewById(R.id.factoryHide);
            input = (EditText) convertView.findViewById(R.id.input);
            search = (ImageView) convertView.findViewById(R.id.search);
            if (mFactoryList.size() == 1) {
                StorageManageMaterielIndexBean.BodyBean factoryItem = mFactoryList.get(0);
                factory.setText(factoryItem.getFty_code() + " - " + factoryItem.getFty_name());
                StorageManageUtils.updateFactoryInfo(mContext, factoryItem.getFty_id(), factoryItem.getFty_code(), factoryItem.getFty_name());
                factoryHide.setVisibility(View.VISIBLE);
                List<StorageManageMaterielIndexBean.BodyBean.LocationAryBean> inventoryAddressList = factoryItem.getLocation_ary();
                if (inventoryAddressList.size() == 1) {
                    StorageManageMaterielIndexBean.BodyBean.LocationAryBean inventoryAddressItem = inventoryAddressList.get(0);
                    inventoryAddress.setText(inventoryAddressItem.getLoc_code() + " - " + inventoryAddressItem.getLoc_name());
                    StorageManageUtils.updateInventoryAddressInfo(mContext, inventoryAddressItem.getLoc_id(), inventoryAddressItem.getLoc_code(), inventoryAddressItem.getLoc_name(), inventoryAddressItem.getWh_id());
                }
                inventoryAddress.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        DialogUtils.getStorageMaterielInventoryAddressDialog(mContext, inventoryAddressList, new DialogUtils.StorageMaterielInventoryAddressListener() {
                            @Override
                            public void onSelect(Dialog dialog, String inventoryAddressIdStr, String inventoryAddressCodeStr, String inventoryAddressNameStr, String warehouseIdStr) {
                                inventoryAddress.setText(inventoryAddressCodeStr + " - " + inventoryAddressNameStr);
                                StorageManageUtils.updateInventoryAddressInfo(mContext, inventoryAddressIdStr, inventoryAddressCodeStr, inventoryAddressNameStr, warehouseIdStr);
                            }
                        }).show();
                    }
                });
            }

            factory.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    DialogUtils.getStorageMaterielFactoryDialog(mContext, mFactoryList, new DialogUtils.StorageMaterielFactoryListener() {
                        @Override
                        public void onSelect(Dialog dialog, String id, String code, String name, List<StorageManageMaterielIndexBean.BodyBean.LocationAryBean> inventoryAddressList) {
                            if (!(code + " - " + name).equals(factory.getText().toString())) {
                                factory.setText(code + " - " + name);
                                StorageManageUtils.updateFactoryInfo(mContext, id, code, name);
                                inventoryAddress.setText(mContext.getResources().getString(R.string.activity_in_storage_other_index_inventory_address_hint));
                                factoryHide.setVisibility(View.VISIBLE);
                                inventoryAddress.setOnClickListener(new NoDoubleClickListener() {
                                    @Override
                                    public void onNoDoubleClick(View v) {
                                        DialogUtils.getStorageMaterielInventoryAddressDialog(mContext, inventoryAddressList, new DialogUtils.StorageMaterielInventoryAddressListener() {
                                            @Override
                                            public void onSelect(Dialog dialog, String inventoryAddressIdStr, String inventoryAddressCodeStr, String inventoryAddressNameStr, String warehouseIdStr) {
                                                inventoryAddress.setText(inventoryAddressCodeStr + " - " + inventoryAddressNameStr);
                                                StorageManageUtils.updateInventoryAddressInfo(mContext, inventoryAddressIdStr, inventoryAddressCodeStr, inventoryAddressNameStr, warehouseIdStr);
                                            }
                                        }).show();
                                    }
                                });
                            }
                        }
                    }).show();
                }
            });
            if (!TextUtils.isEmpty(mCondition)) {
                input.setText(mCondition);
            }
            if (!TextUtils.isEmpty(mFactoryCode) && !TextUtils.isEmpty(mFactoryName)) {
                factory.setText(mFactoryCode + " - " + mFactoryName);
                factoryHide.setVisibility(View.VISIBLE);
            }
            if (!TextUtils.isEmpty(mInventoryAddressCode) && !TextUtils.isEmpty(mInventoryAddressName)) {
                inventoryAddress.setText(mInventoryAddressCode + " - " + mInventoryAddressName);
            }
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    String conditionStr = input.getText().toString();
                    if (TextUtils.isEmpty(conditionStr)) {
                        Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_refund_storage_picking_list_the_query_condition_must_not_be_empty));
                        return;
                    }
                    StorageMaterielBean storageBean = StorageManageUtils.getStorageMaterielBean(mContext);
                    if (TextUtils.isEmpty(storageBean.getFactoryId()) || TextUtils.isEmpty(storageBean.getInventoryAddressId())) {
                        Utils.middleToast(mContext, mContext.getResources().getString(R.string.activity_storage_manage_matr_please_select_factory_and_stock_location));
                        return;
                    }
                    mStorageManageMaterielListener.onNoDoubleClickListener(conditionStr, storageBean.getWarehouseId());
                }
            });
        } else {
            StorageManageMatrAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                //构造列表项控件，用ItemView进行缓存
                convertView = inflater.inflate(R.layout.view_storage_manage_list_item, null);
                viewHolder = new StorageManageMatrAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                //复用控件
                viewHolder = (StorageManageMatrAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            StorageManageMatrBean.BodyBean item = mMaterielList.get(position - 1);
            initData(mContext, viewHolder, convertView, item);
        }
        return convertView;
    }

    private void initView(StorageManageMatrAdapter.ViewHolder viewHolder, View view) {
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.specialInventoryCode = (TextView) view.findViewById(R.id.specialInventoryCode);
        viewHolder.materielDescription = (TextView) view.findViewById(R.id.materielDescription);
        viewHolder.sendPosition = (TextView) view.findViewById(R.id.sendPosition);
        viewHolder.batchCode = (TextView) view.findViewById(R.id.batchCode);
        viewHolder.inputText = (TextView) view.findViewById(R.id.inputText);
        viewHolder.specialInventoryCodeTextView = (TextView) view.findViewById(R.id.specialInventoryCodeTextView);
        viewHolder.firstRow = (PercentRelativeLayout) view.findViewById(R.id.firstRow);
    }

    private void initData(Context context, StorageManageMatrAdapter.ViewHolder viewHolder, View view, StorageManageMatrBean.BodyBean item) {
        viewHolder.materielCode.setText(item.getMat_code());
        viewHolder.specialInventoryCode.setText(item.getSpec_stock_code());
        viewHolder.materielDescription.setText(item.getMat_name());
        viewHolder.sendPosition.setText(item.getS_position_code());
        viewHolder.batchCode.setText(item.getBatch());

        // 特殊库存编码为空则隐藏
        if (TextUtils.isEmpty(item.getSpec_stock_code())) {
            viewHolder.specialInventoryCode.setVisibility(View.INVISIBLE);
            viewHolder.specialInventoryCodeTextView.setVisibility(View.INVISIBLE);
        }

        if (item.getStoragePositionList() != null && item.getStoragePositionList().size() > 0) {
            viewHolder.inputText.setText(StorageManageUtils.getStorageMaterielCollateNumber(item) + item.getUnit_name());
            viewHolder.firstRow.setBackgroundColor(ContextCompat.getColor(context, R.color.base_green));
        } else {
            //因为有可能会复用之前的渲染，需要根据数据重新设置组件的可见性
            viewHolder.inputText.setText(context.getString(R.string.view_storage_manage_list_item_inputText));
            viewHolder.firstRow.setBackgroundColor(ContextCompat.getColor(context, R.color.base_gray));
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(mContext, StorageManageMatrMoveActivity.class);
                intent.putExtra(StorageManageExtra.ID, item.getId());
                mContext.startActivity(intent);
            }
        });
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                DialogUtils.getConfirmDialog(context, view.getResources().getString(R.string.activity_storage_manage_matr_delete_is_enconded_as) + viewHolder.materielCode.getText().toString() +
                                view.getResources().getString(R.string.activity_storage_manage_matr_material),
                        new DialogUtils.ConfirmDialogListener() {
                            @Override
                            public void onConfirm(Dialog dialog) {
                                dialog.cancel();
                                StorageManageUtils.deleteMateriel(context, viewHolder.materielCode.getText().toString(), viewHolder.sendPosition.getText().toString(), viewHolder.batchCode.getText().toString());
                                mStorageManageMaterielListener.onDeleteMateriel();
                            }
                        }).show();
                return true;
            }
        });
    }

    private class ViewHolder {
        private TextView materielCode, specialInventoryCodeTextView, specialInventoryCode, materielDescription, sendPosition, batchCode, inputText;
        private PercentRelativeLayout firstRow;
    }

    public void setList(List<StorageManageMatrBean.BodyBean> list) {
        this.mMaterielList = list;
    }
}
